package view;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

/**
 * This is the Pending Request GUI extends AbstractGUI
 * @since 04/06/2014
 * @author Tomer Elkayam
 */
public class SearchResultGUI extends AbstractGUI
{
	private JPanel contentPane;
	private JTable table;
	private JScrollPane sp;
	private ArrayList<ArrayList<JButton>> buttons;
	private int nButtons;
	private String[] buttonNames;
	
	/**
	 * This is SearchResultGUI constructor.
	 * @param title - the window title
	 */
	public SearchResultGUI(String title)
	{
		///main Panel   
		contentPane = new JPanel();
		
     	contentPane.setBorder(new TitledBorder(null, title, TitledBorder.LEADING, TitledBorder.TOP, null, null));
     	contentPane.setLayout(null);
	    contentPane.setOpaque(true);
	    contentPane.add(getBackButton());
	    this.setContentPane(contentPane);
		       
        //Frame
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.setTitle(title);
        this.setContentPane(contentPane);   
        this.setLocationRelativeTo(null); //This will center the JFrame in the middle of the screen
        this.setLocationByPlatform(true);
        this.setSize(760, 640);
	}
	
	/***
	 * create a new table and populate it with data
	 * @param columnNames - an array of colum names
	 * @param data - an array of data objects
	 * @author Tomer Elkayam
	 */
	public void setTable(String[] columnNames, Object [][] data)
	{
		//dispose any existing tables
		disposeTable();
		
		table = getTableObject();
		//adjust data from db into table 
		DefaultTableModel def=new DefaultTableModel(data,columnNames);
		
		table.setModel(def);
		//update table
		def.fireTableDataChanged();
		
		//change color header
		JTableHeader header = table.getTableHeader();
	      header.setBackground(Color.YELLOW);
		
		
		//create scrollpane with the new Table
		sp = getScrollPaneObject();
		sp.setViewportView(table);		
		getContentPane().add(sp);
	}

	
	/**
	 * This method removes the dynamically allocated buttons from the content pane.
	 */
	public void disposeExistingButtons()
	{
		if(buttons == null)
			return;
		
		for(int i = 0 ; i < buttons.size(); i++)
	    {
			ArrayList<JButton> buttonList = buttons.get(i);
			
	    	for (int j = 0; j < nButtons; j++) 
			{
				getContentPane().remove(buttonList.get(j));
				getContentPane().validate();
				getContentPane().repaint();
	    		
			}
	    }
	}
	
	/**
	 * This method sets vertical lists of buttons according to number of button names given.
	 *@param buttonNames - names of buttons
	 *@param nButtons - number of Buttons to Create
	 */
	public void setAllbuttons(int nButtons , String[] buttonNames)
	{
		
		disposeExistingButtons();
		
		//set all  buttons
		int y=0,x=0,btnWidth = 120,btnHight = 23;
		
		this.nButtons = nButtons;
		this.buttonNames = buttonNames;
		buttons = new ArrayList<ArrayList<JButton>>();
	    
	    for(int i = 0 ; i < nButtons; i++)
	    {
	    	ArrayList<JButton> bi = new ArrayList<JButton>();
	    	
	    	for (int j = 0; j < buttonNames.length; j++) 
			{
	    		//create buttons
	    		JButton b = new JButton(buttonNames[j]);
	    		//set properties
		    	b.setBounds(458 + x, 105 + y, btnWidth, btnHight);
		    	b.setVisible(true);
		    	//add to containers
		    	bi.add(b);
		    	contentPane.add(b);
		        //inc next button x position
		        x+=btnWidth+6;
			}
	    	x=0;
	    	buttons.add(bi);
	    	 //inc next button y position
	    	y+=30;
	   }

		getContentPane().validate();
		getContentPane().repaint();
	    
	}
	

	/**
	 * This method adds action listener to Add Photo button.
	 * @param listener
	 */
	public void addButtonListener(ActionListener listener ,String btnType, int buttonNumber)
	{
		for (int i = 0; i < buttonNames.length; i++) 
			if(buttonNames[i].equals(btnType))
			{
				buttons.get(buttonNumber).get(i).addActionListener(listener);
				buttons.get(buttonNumber).get(i).setActionCommand(""+buttonNumber);
				return;
			}
	}
	
	/**
	 * This method disposes the allocated table.
	 */
	public void disposeTable()
	{
		if(table!=null)
		{
			table.setVisible(false);
			table = null;
			getContentPane().remove(sp);
		}
	}
	
	
	/**
	 * This method sets the table visibility.
	 * @param boolean b visibility status
	 */
	
	public void setTableVisibilty(boolean b)
	{
		table.setVisible(b);
	}
	
	/**
	 * This method return -simulate back button pressed .
	 * @param listener
	 */
	public void clickBackButton()
	{
		this.getBackButton().doClick();
	}
	
	
	/**
	 * This method initializes Table
	 */
	private JTable getTableObject() 
	{
		if (table == null) 
		{
			    table = new JTable();
		        table.setColumnSelectionAllowed(true);
		        table.setFillsViewportHeight(true);
		        table.setCellSelectionEnabled(true);
		        table.setSurrendersFocusOnKeystroke(true);
		     
		       
		        table.setBounds(0,0, 500-50, 150-165);
		        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		        table.setFont(table.getFont().deriveFont(11f));
		 	      
		       
		        table.setBackground(Color.WHITE);
		        table.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		        table.setRowHeight(30);
		}
		return table;
	}
	
	/**
	 * This method initializes JScrollPane to contain for the JTable
	 */
	private JScrollPane getScrollPaneObject()
	{
	     sp = new JScrollPane();
		 sp.setViewportBorder(new CompoundBorder());
		 sp.setBounds(50, 83, 398, 261);

		return sp;
	}
	
	
	/**
	 * This method initializes Back Button	
	 */
	private JButton getBackButton() 
	{
		if (backButton == null) 
		{
			backButton = new JButton();
			backButton.setBounds(new Rectangle(530, 505, 123, 35));
			backButton.setText("Back to main");
			
		}
		return backButton;
	}
	
}
